﻿using Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Insteon
{
    [DataContract]
    public class Command: ICommand
    {
        public Command(string id, string command, string level = null)
        {
            DeviceID = id;
            Level = level;
            Cmd = command;
            HexString = level == null ? string.Format(command, DeviceID): 
                                            string.Format(command, DeviceID, level);
        }

        [DataMember]
        public byte[] ByteArray
        {
            get
            {
                List<byte> command = new List<byte>();
                string[] hexparts = HexString.Split(' ');

                foreach (string part in hexparts)
                {
                    int temp = Convert.ToInt32(part, 16);
                    char c = (char)temp;
                    command.Add(Convert.ToByte(c));
                }

                return command.ToArray();
            }
            set {/*Do Nothing*/}
        }

        [DataMember]
        public int Count
        {
            get { return HexString.Split(' ').Count(); }
            set {/*Do Nothing*/ }
        }

        [DataMember]
        public string HexString
        {
            get;
            set;
        }

        [DataMember]
        public string DeviceID
        {
            get;
            set;
        }

        [DataMember]
        public string Cmd { get; set; }

        [DataMember]
        public string Level { get; set; }
    }
}
